#!/usr/bin/env node

const fs = require('fs')
const path = require('path')
const inquirer = require('inquirer')
const ejs = require('ejs')

inquirer.prompt([
    {
        type: 'input',
        name: 'name',
        msg: 'project name?'
    }
]).then(answers => {
    const tempPath = path.join(__dirname, 'templates')
    const destPath = process.cwd()
    const paths = []

    const templates = [
        '.browserslistrc',
        '.editorconfig',
        '.env.development',
        '.env.production',
        '.eslintrc.js',
        '.gitignore',
        'babel.config.js',
        'package.json',
        'postcss.config.js',
        'README.md',
        'public/favicon.ico',
        'public/index.html',
        'src/App.vue',
        'src/main.js',
        'src/router.js',
        'src/assets/logo.png',
        'src/components/HelloWorld.vue',
        'src/store/actions.js',
        'src/store/getters.js',
        'src/store/index.js',
        'src/store/mutations.js',
        'src/store/state.js',
        'src/utils/request.js',
        'src/views/About.vue',
        'src/views/Home.vue'
    ]
    templates.forEach(file => {
        ejs.renderFile(path.join(tempPath, file), answers, (err, result) => {
            if (err) throw err
            let targetPath = path.join(destPath, file)
            let lastPath = targetPath.substring(0, targetPath.lastIndexOf('\\'))
            fs.mkdir(lastPath, { recursive : true }, err => {
                if (err) throw err
                fs.writeFileSync(path.join(destPath, file), result)
            })
            
        })
    })
})
